<?php
require_once '../../global.inc.php';
$i = $_GET["del"];

if (!empty($_POST['submit'])) {
 $upload = new UploadFile($_FILES['files'], '../../upload');
 //上传用户文件，返回int值，为上传成功的文件个数。
 $num = $upload->upload();
 if ($num != 0) {
 	$str =$num."个文件上传成功！";
 	echo "<script>alert('$str');</script>";
 	$attachmentArray = $upload->getSaveInfo();
 	//保存到session
 	foreach ($attachmentArray as $attachmentInfo){
 		$_SESSION["attachmentArray"][] = $attachmentInfo;
 	}
 }
}else if(isset($i)){//如果传入的i已经有需要删除的节点下标，那么删除session中的节点。
	array_splice($_SESSION["attachmentArray"], $i,1);	
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
<style type="text/css">
<!--
body {
	margin-left: 3px;
	margin-top: 0px;
	margin-right: 3px;
	margin-bottom: 0px;
}
.input1 {
	width:100%;
}
textarea {
	width:100%;
	resize:none;
	font-size: 12px;
}
div{margin:0 10px}
.STYLE1  {color: #e1e2e3; font-size: 12px;}
.STYLE6  {color: #000000; font-size: 12px;}
.STYLE10 {color: #000000; font-size: 12px;}
.STYLE19 {color: #344b50; font-size: 12px;}
.STYLE21 {color: #3b6375; font-size: 12px;}
.STYLE22 {color: #295568; font-size: 12px;}

-->
</style>
<script language="javascript" type="text/javascript">
function addInput(){
	var input=document.createElement('input');//创建一个input节点
	var br=document.createElement('br');//创建一个br节点
	input.setAttribute('type','file');//设置input节点type属性为file
	input.setAttribute('name','files[]');//设置input节点name属性为files[]，以 数组的方式传递给服务器端
	var obj = document.getElementById("div1");//把节点添加到div1中
	obj.appendChild(br);
	obj.appendChild(input);
}
</script>
</head>

<body>

<div align="center"><h3>附件上传</h3></div>
<form name="myForm" enctype="multipart/form-data" method="POST" action="" >
<table align="center" width="90%" border="0" cellpadding="3" cellspacing="1" bgcolor="#a8c7ce">
	<tr><td  bgcolor="#FFFFFF" class="STYLE6"><div id="div1" align="center">
		<input type="file" name="files[]">
	</div></td></tr>
	<tr><td bgcolor="#FFFFFF" class="STYLE6"><div align="center">
		<input type="button" name="button" value="增加附件" onclick="addInput()">
	 	<input type="submit" name="submit" value="上传附件">
	</div></td></tr>
	<tr><td bgcolor="#FFFFFF" class="STYLE6"><div align="left">
	已上传附件：<br/>
	<?php 
	if(!empty($_SESSION["attachmentArray"])){
		$i=0;
		foreach ($_SESSION["attachmentArray"] as $attachmentInfo){
			$fileName = $attachmentInfo["file_name"];
			$filePath = $attachmentInfo["file_path"];
			echo "&nbsp;&nbsp;【".$fileName."】<a href='upload.php?del=".$i."'><img src='../images/delete.jpg'  title='删除附件'/> </a><br/>";
			$i++;
		}
	}
	?>
	</div></td></tr>
</table>

</form>
</body>
</html>